﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigCopyToOnPremises.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace AdminTool
{
    using System;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    using ExecutionTool.CopyOnPremises;
    using ExecutionTool.Profile;

    public partial class ConfigCopyToOnPremises : UserControl, IProfileConfigurator
    {
        #region Constructors and Destructors

        public ConfigCopyToOnPremises()
        {
            this.InitializeComponent();

            Program.ExportEnablementChanged += (sender, args) =>
                {
                    if (!args.Enabled)
                    {
                        this.checkBoxEnableCopyOnPremises.Checked = args.Enabled;
                    }
                };

            this.checkBoxEnableCopyOnPremises.Checked = false;
        }

        #endregion

        #region Public Methods and Operators

        public void LoadProfile(Profile profile)
        {
            this.checkBoxEnableCopyOnPremises.Checked = profile.Orchestration.CopyToOnPremises;
            this.textBoxLocation.Text = profile.TargetOnPremisesLocation.Location;
            this.textBoxDescription.Text = profile.TargetOnPremisesLocation.Description;

            this.ResetMessage();
            this.WriterMessage("Profile data loaded.");
        }

        public void SaveProfile(Profile profile)
        {
            profile.Orchestration.CopyToOnPremises = this.checkBoxEnableCopyOnPremises.Checked;
            profile.TargetOnPremisesLocation.Location = this.textBoxLocation.Text.Trim();
            profile.TargetOnPremisesLocation.Description = this.textBoxDescription.Text.Trim();

            this.WriterMessage("Profile data saved.");
        }

        #endregion

        #region Methods

        private void ConfigCopyToOnPremises_Load(object sender, EventArgs e)
        {
        }

        private void ResetMessage()
        {
            this.textBoxTestConnection.Text = string.Empty;
        }

        private void WriterMessage(string message)
        {
            this.textBoxTestConnection.Text += message;
            this.textBoxTestConnection.Text += Environment.NewLine;
        }

        private void buttonTestConnection_Click(object sender, EventArgs e)
        {
            var location = this.textBoxLocation.Text.Trim();

            if (string.IsNullOrWhiteSpace(location))
            {
                this.WriterMessage("Please specify the location first.");
                return;
            }

            this.textBoxTestConnection.Text = string.Empty;
            this.WriterMessage("Starting to test the location...");
            var adapter = new OnPremisesAdapter();
            Task.Factory.StartNew(() => adapter.TestConnection(new OnPremisesLocation { Location = location })).ContinueWith(
                t =>
                    {
                        if (t.IsFaulted && t.Exception != null)
                        {
                            this.WriterMessage("Test failed. -->" + t.Exception);
                        }
                        else
                        {
                            this.WriterMessage("Tested successfully.");
                        }
                    });
        }

        private void checkBoxEnableCopyOnPremises_CheckedChanged(object sender, EventArgs e)
        {
            var cb = (CheckBox)sender;
            this.groupBox1.Enabled = cb.Checked;
        }

        #endregion
    }
}
